<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="省" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区/县" prop="district">
        <el-input
          v-model="queryParams.district"
          placeholder="请输入区/县"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医院名称" prop="hospitalName">
        <el-input
          v-model="queryParams.hospitalName"
          placeholder="请输入医院名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收益%" prop="gainSharing">
        <el-input
          v-model="queryParams.gainSharing"
          placeholder="请输入收益%"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:hospital:add']"
        >新增医院</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:hospital:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:hospital:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:hospital:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hospitalList" @selection-change="handleSelectionChange">
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区/县" align="center" prop="district" />
      <el-table-column label="对接人名字" align="center" prop="duijie" />
      <el-table-column label="医院名称" align="center" prop="hospitalName" />
      <el-table-column label="租金" align="center" prop="rent" />
      <el-table-column label="医院银行卡号" align="center" prop="cardNo" />
      <el-table-column label="收益%" align="center" prop="gainSharing" />
      <el-table-column label="股东姓名" align="center" prop="gudong" />
      <el-table-column label="一级分销商姓名" align="center" prop="yiji" />
      <el-table-column label="一级分销商名称" align="center" prop="erji" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:hospital:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:hospital:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改医院信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区/县" prop="district">
          <el-input v-model="form.district" placeholder="请输入区/县" />
        </el-form-item>
        <el-form-item label="对接人ID" prop="userId" v-if="false">
          <el-input v-model="form.userId" placeholder="请输入对接人ID" />
        </el-form-item>

      <el-form-item label="对接人">
              <el-select v-model="form.userId" placeholder="请选择对接人">
                <el-option
                  v-for="item in duijieList"
                  :key="item.userId"
                  :label="item.duijie"
                  :value="item.userId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
         </el-form-item>

        <el-form-item label="医院名称" prop="hospitalName">
          <el-input v-model="form.hospitalName" placeholder="请输入医院名称" />
        </el-form-item>
        <el-form-item label="租金" prop="rent">
          <el-input v-model="form.rent" placeholder="请输入租金" />
        </el-form-item>
        <el-form-item label="医院银行卡号" prop="cardNo">
          <el-input v-model="form.cardNo" placeholder="请输入医院银行卡号" />
        </el-form-item>
        <el-form-item label="收益%" prop="gainSharing">
          <el-input v-model="form.gainSharing" placeholder="请输入收益%" />
        </el-form-item>
        <el-form-item label="股东id" prop="shareholderId" v-if="false">
          <el-input v-model="form.shareholderId" placeholder="请输入股东id" />
        </el-form-item>
         

        <el-form-item label="股东">
              <el-select v-model="form.shareholderId" placeholder="请选择对股东">
                <el-option
                  v-for="item in gudongList"
                  :key="item.userId"
                  :label="item.gudong"
                  :value="item.userId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
         </el-form-item>


        <el-form-item label="一级分销商id" prop="distributor1" v-if="false">
          <el-input v-model="form.distributor1" placeholder="请输入一级分销商id" />
        </el-form-item>
 
        <el-form-item label="一级分销商">
              <el-select v-model="form.distributor1" placeholder="请选择对一级分销商">
                <el-option
                  v-for="item in yijiList"
                  :key="item.userId"
                  :label="item.yiji"
                  :value="item.userId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
         </el-form-item>

        <el-form-item label="二级分销商id" prop="distributor2" v-if="false">
          <el-input v-model="form.distributor2" placeholder="请输入二级分销商id" />
        </el-form-item>

        <el-form-item label="二级分销商">
              <el-select v-model="form.distributor2" placeholder="请选择二级分销商">
                <el-option
                  v-for="item in erjiList"
                  :key="item.userId"
                  :label="item.erji"
                  :value="item.userId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
         </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHospital, getHospital, delHospital, addHospital, updateHospital } from "@/api/admin/hospital";
import {listUser } from "@/api/system/user";
export default {
  name: "Hospital",
  data() {
    return {

      //对接人姓名列表，用于添加修改
      userList:[],


      duijieList:[],
      gudongList:[],
      yijiList :[],
      erjiList:[],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 医院信息表格数据
      hospitalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        province: null,
        city: null,
        district: null,
        userId: null,
        hospitalName: null,
        rent: null,
        cardNo: null,
        gainSharing: null,
        shareholderId: null,
        distributor1: null,
        distributor2: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shareholderId: [
          { required: true, message: "股东id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询医院信息列表 */
    getList() {
      this.loading = true;
      listHospital(this.queryParams).then(response => {
        this.hospitalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hospitalId: null,
        province: null,
        city: null,
        district: null,
        userId: null,
        hospitalName: null,
        rent: null,
        cardNo: null,
        gainSharing: null,
        createTime: null,
        shareholderId: null,
        distributor1: null,
        distributor2: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hospitalId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },


    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getHospital().then(response => {
        //查询所有用户加角色
        //this.userList = response.rows;
        this.duijieList = response.duijie;
        this.gudongList = response.gudong;
        this.yijiList = response.yiji;
        this.erjiList = response.erji;
        this.open = true;
        this.title = "添加医院";
      });

      


    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const hospitalId = row.hospitalId || this.ids
      getHospital(hospitalId).then(response => {
        this.form = response.xiugai;
        this.duijieList = response.duijie;
        this.gudongList = response.gudong;
        this.yijiList = response.yiji;
        this.erjiList = response.erji;
        this.open = true;
        this.title = "修改医院信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.hospitalId != null) {
            updateHospital(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHospital(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hospitalIds = row.hospitalId || this.ids;
      this.$modal.confirm('是否确认删除医院信息编号为"' + hospitalIds + '"的数据项？').then(function() {
        return delHospital(hospitalIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('admin/hospital/export', {
        ...this.queryParams
      }, `hospital_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
